package com.lzs.adx.web.commmon.support;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.lzs.adx.commmon.utils.DateUtils;
import com.lzs.adx.web.modules.adx.dto.TotalStatisticsDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
public class CustomExcelListener extends AnalysisEventListener<Map<Integer, String>> {
    private List<TotalStatisticsDTO> dataList = new ArrayList<>();

    @Override
    public void invoke(Map<Integer, String> rowMap, AnalysisContext context) {
        String firstColumnValue = rowMap.get(0);
        //解析每行数据的时候，如果遇到空格，或者特定的名称，直接跳过
        if (StringUtils.isEmpty(firstColumnValue) || firstColumnValue.contains("产品名称")) {
            log.info("跳过了当前行：{}", firstColumnValue);
            return;
        }


        String day = rowMap.get(5);
        Date parse = DateUtils.parse(day, DateUtils.DATE_PATTERN);
        if(parse.after(new Date())){
            log.info("跳过了当前行：{}", firstColumnValue);
            return;
        }



        try {
            TotalStatisticsDTO dto = convertToDTO(rowMap);
            dataList.add(dto);
            log.info("处理数据: {}", dto);
        } catch (Exception e) {
            log.error("数据转换异常，跳过该行: {}", rowMap, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成！");
    }

    public List<TotalStatisticsDTO> getDataList() {
        return dataList;
    }

    private TotalStatisticsDTO convertToDTO(Map<Integer, String> rowMap) {
        TotalStatisticsDTO dto = new TotalStatisticsDTO();
        dto.setProduct(rowMap.get(0));
        dto.setPartyChannel(rowMap.get(1));
        dto.setChannel(rowMap.get(2));
        dto.setSecondChannel(rowMap.get(3));
        dto.setProductCode(rowMap.get(4));
        dto.setDay(rowMap.get(5));
        dto.setResultNumber(StringUtils.isNotBlank(rowMap.get(6))?new BigDecimal(rowMap.get(6)):new BigDecimal(0));
        dto.setPartyPrice(StringUtils.isNotBlank(rowMap.get(7))?new BigDecimal(rowMap.get(7)):new BigDecimal(0));
        dto.setPartySetter(StringUtils.isNotBlank(rowMap.get(8))?new BigDecimal(rowMap.get(8)):new BigDecimal(0));
        dto.setSecondPrice(StringUtils.isNotBlank(rowMap.get(9))?new BigDecimal(rowMap.get(9)):new BigDecimal(0));
        dto.setSecondSetter(StringUtils.isNotBlank(rowMap.get(10))?new BigDecimal(rowMap.get(10)):new BigDecimal(0));
        dto.setProfit(StringUtils.isNotBlank(rowMap.get(11))?new BigDecimal(rowMap.get(11)):new BigDecimal(0));
        dto.setDirectBusiness(rowMap.get(12));
        dto.setJoinBusiness(rowMap.get(13));
        dto.setOperate(rowMap.get(14));
        dto.setType(rowMap.get(15));
        dto.setRemark(rowMap.get(16));
        return dto;
    }
}